﻿
$(document).ready(function () {
	SetTootipsExtensions();
});

/* Tooltip selector's getters */

function GetPersonInfoTooltipSelector() {

	return "#personInfoTooltip";
}
function GetPersonTimeTooltipSelector() {

	return "#genericTooltip";
}

function GetGenericTooltipSelector() {

	return "#genericTooltip";
}

/* End */
var isRegistredInfoTooltip = false;
/* Tooltips extensions */
function SetTootipsExtensions() {

	$.fn.personInfoTooltip = function(elementSelector) {

		RegisterElementHandlers(this, elementSelector, GetPersonInfoTooltipSelector(), false);
		RegisterTooltipHandlers(GetPersonInfoTooltipSelector(), this, false);

		return this;
	};

	$.fn.personTimeTooltip = function(elementSelector) {

		RegisterElementHandlers(this, elementSelector, GetPersonTimeTooltipSelector(), true);
		RegisterTooltipHandlers(GetPersonTimeTooltipSelector(), this, true);

		return this;
	};

	$.fn.commonTooltip = function(elementSelector) {

		RegisterElementHandlers(this, elementSelector, GetGenericTooltipSelector(), true);
		RegisterTooltipHandlers(GetGenericTooltipSelector(), this, true);

		return this;
	};
}

/* End */

/* Tooltip extension's helpers */

function RegisterElementHandlers(container, elementSelector, tooltip, isGenericTooltip) {

	$(container).on("mouseenter mouseleave", elementSelector, function (e) { OnPersonElementHasMouseChange(e, container, tooltip, isGenericTooltip); });
	$(container).on("mousedown", elementSelector, function (e) { OnElementMouseDown(e, tooltip); });
}
function RegisterTooltipHandlers(tooltip,container,isGenericTooltip) {

	$("body").on("mouseenter mouseleave", tooltip, function (e) { OnTooltipMouseChange(e, tooltip, container, isGenericTooltip); });
}

/* End */

/* Tooltip's element */

function OnPersonElementHasMouseChange(e, container, tooltip, isGenericTooltip) {

	var tooltipObj = $(tooltip+":not(:hidden)");

	if (e.type == "mouseenter") {

		if ($(tooltipObj).length != 0) {
			DestroyTooltip(tooltipObj);
		}

		$(e.target).data("ajax-request-timer", setTimeout(function () { GenerateTooltipAjaxRequest(e.target, tooltip,isGenericTooltip,GetTooltipPos(tooltip,e.pageX,e.pageY,e.target),GetAjaxParams(e.target)); }, 1000));
	}
	else {
		clearTimeout($(e.target).data("ajax-request-timer"));

		var timeoutTime = isGenericTooltip ? 0 : 500;

		if ($(tooltipObj).length != 0) {
			$(tooltipObj).data("tooltip-timer", setTimeout(function () { DestroyTooltip(tooltip); }, timeoutTime));
		}
	}
}

function OnElementMouseDown(e, tooltip) {

	clearTimeout($(e.target).data("ajax-request-timer"));
	DestroyTooltip(tooltip);
}

function GenerateTooltipAjaxRequest(target, tooltip,isGenericTooltip,tooltipPos,ajaxParams) {

		var tooltipObj = $(tooltip);
		var smallSpinnerObj = $("#smallSpinner");

		if (!$(smallSpinnerObj).is(":hidden") && $(smallSpinnerObj).parent().hasClass("tooltip")) {
			DestroyTooltip($(smallSpinnerObj).parent());
		}

		$(tooltipObj).empty();

		$(tooltipObj).css("left", tooltipPos.left);

		$(tooltipObj).css("top", tooltipPos.top);

		$(tooltipObj).append(smallSpinnerObj);

		if (!isGenericTooltip) {

			$(smallSpinnerObj).show();
		}
		
		$(tooltipObj).show();

	$(tooltipObj).data("requestXhr", $.ajax({
		url: ajaxParams.requestUrl,
		type: "POST",
		data: ajaxParams.requestData,
		complete: function (xhr, textStatus) { OnTooltipInfoLoadComplete(xhr, textStatus, target); }
	}));
}

function GetAjaxParams(target) {

	var ajaxParams = new Object();

	ajaxParams.requestData = new Object();
	ajaxParams.requestUrl = "";

	if ($(target).hasClass("clockIcon")) {

		ajaxParams.requestData.personID = $(target).siblings(".peopleName").data("person-id");
		ajaxParams.requestData.bookID = $(target).closest(".bookContent").find("input[name='bookID']").val();
		ajaxParams.requestData.subscriptionSet = $(target).parent().data("subscription-set");
	}
	else if ($(target).hasClass("peopleName")) {

		ajaxParams.requestData.personID = $(target).data("person-id");
	}
	else {

		ajaxParams.requestData.tooltipType = $(target).data("tooltip-type");
	}

	ajaxParams.requestUrl = $(target).data("tooltip-url");

	return ajaxParams;
}

/* End */

/* Tooltips */

function OnTooltipMouseChange(e,tooltip, container, isGenericTooltip) {

	if (e.type == "mouseenter") {
		clearTimeout($(tooltip).data("tooltip-timer"));
	}
	else {
		var timeoutTime = isGenericTooltip ? timeoutTime = 0 : timeoutTime = 500;
		$(tooltip).data("tooltip-timer", setTimeout(function () { DestroyTooltip(tooltip); }, timeoutTime));
	}
}

function OnTooltipInfoLoadComplete(xhr, textStatus, target) {

	var tooltip = $("#"+$(target).data("tooltip-id"));

	if (textStatus == "success" && !$(tooltip).is(":hidden")) {

		var tooltipElement = xhr.responseText;

		$("#smallSpinner").hide().appendTo("body");

		$(tooltip).html(tooltipElement);
	}
}

function GetTooltipPos(tooltip, pageX, pageY, target) {

	var tooltipPos = new Object();

	var tooltipOffsetX = 20;
	var tooltipOffsetY = 0;

	var targetHeight = $(target).height();

	var browserWidth = $(window).width();
	var browserHeight = $(window).height();

	var tooltipWidth = $(tooltip).width();
	var tooltipHeight = $(tooltip).height();

	tooltipPos.left = pageX + tooltipOffsetX;
	tooltipPos.top = pageY + tooltipOffsetY;


	if (browserHeight > tooltipPos.top + tooltipHeight) {

		if (tooltipPos.left + tooltipWidth > browserWidth && pageX - tooltipOffsetX - tooltipWidth > 0) {

			tooltipPos.left = tooltipPos.left - 2 * tooltipOffsetX - tooltipWidth;
		}
	} else {

		if (tooltipPos.left + tooltipWidth > browserWidth && pageX - tooltipOffsetX - tooltipWidth > 0) {

			tooltipPos.left = tooltipPos.left - 2 * tooltipOffsetX - tooltipWidth;
		}

		tooltipPos.top = tooltipPos.top - tooltipOffsetY - targetHeight - tooltipHeight;
	}

	return tooltipPos;
}

function DestroyTooltip(tooltip) {

	var smallSpinnerObj = $("#smallSpinner");
	var tooltipObj = $(tooltip);

	var requestXhr = $(tooltipObj).data("requestXhr");
	if (requestXhr != null) {
		requestXhr.abort();
		$(tooltipObj).removeData("requestXhr");
	}

	if ($(tooltip).find(smallSpinnerObj).length != 0) {
		$(smallSpinnerObj).hide().appendTo("body");
	}
	$(tooltipObj).hide();
	$(tooltipObj).empty();
}
/* End */